<?php include("php/dao/DatabaseDAO.php");
include("php/Security.php");

Security::checkAuthenticated();

$databaseDAO = new DatabaseDAO();
$databases = $databaseDAO->getUserDatabases($_SESSION["UserId"]);
?>

<!DOCTYPE html>
<html>
<head>
    <title>Manage User Databases</title>

    <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
    <script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
    <link rel="stylesheet" href="css/style.css"/>
</head>
<body>
<h1><a href="manage.php">Manage feeds</a> | <a href="add_new_database.php">Manage Databases</a></h1>

<h2>List of user databases:</h2>


<script>
    $(document).ready(function () {
        $("#addNewDatabase").click(function () {
            $("#addDatabaseFormContainer").show();
        });

        $("#testConnectionButton").click(function () {
            var databaseUrl = $("#databaseUrl").val();
            var databaseName = $("#databaseName").val();
            var databaseUser = $("#user").val();
            var databasePassword = $("#password").val();

            $.ajax({
                dataType: "text",
                url: "php/TestDatabaseConnection.php",
                data: {databaseUrl: databaseUrl, databaseName: databaseName,
                    databaseUser: databaseUser, databasePassword: databasePassword},
                type: "POST",
                success: function (result) {
                    if (result == "1") {
                        alert("Connection successful!");
                    } else {
                        alert("Connection failure!");
                    }
                },
                error: function (error) {
                    alert("Connection failure! Error: " + error);
                }
            });
        });
    });
</script>
<div id="listOfDatabases">
    <?php
    echo "<table class='names'>
    <tr>
        <th>Id</th>
        <th>URL</th>
        <th>Database Name</th>
        <th>User</th>
        <th>Edit</th>
        <th>Delete</th>
    </tr>";
    while ($row = mysqli_fetch_assoc($databases)) {
        echo "<tr>";
        echo "<td>" . $row['Id'] . "</td>";
        echo "<td>" . $row['URL'] . "</td>";
        echo "<td>" . $row['DatabaseName'] . "</td>";
        echo "<td>" . $row['User'] . "</td>";
        echo "<td><a href='edit_database.php?databaseId=" . $row['Id'] . "'>edit</a> </td>";
        echo "<td><a href='php/DeleteDatabase.php?databaseId=" . $row['Id'] . "'>delete</a> </td>";
        echo "</tr>";
    }
    echo "</table>"
    ?>

    <input id="addNewDatabase" type="button" class="button" value="Add new..."/>
</div>

<div id="addDatabaseFormContainer" style="display: none;">
    <form id="addFeedForm" action="php/CreateDatabase.php" method="post">
        <label>URL:</label>
        <input type="text" name="databaseUrl" id="databaseUrl" placeholder="Input database URL" required/></br>

        <label>Database name:</label>
        <input type="text" name="databaseName" id="databaseName" placeholder="Input database name" required/></br>

        <label>User:</label>
        <input type="text" name="user" id="user" placeholder="Input username" required/></br>

        <label>Password:</label>
        <input type="password" name="password" id="password" placeholder="Input password" required/></br>

        <input id="testConnectionButton" type="button" class="button" value="Test connection">
        <input id="saveDatabaseButton" type="submit" class="button" value="Save">
    </form>
</div>
</body>
</html>